/**
 * 项目流程控制器 - 服务端
 */
const BaseProjectController = require('./base_project_controller.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const config = require('../../../config/config.js');
const dbUtil = require('../../../framework/database/db_util.js');

class ProjectFlowController extends BaseProjectController {

	/**
	 * 获取项目流程历史
	 * @param {*} event 
	 * @param {*} request
	 */
	async getFlowHistory(event, request) {
		// 提取参数
		let { projectId } = event;

		if (!projectId) {
			return { code: -1, msg: '项目ID不能为空' };
		}

		try {
			// 构建查询条件
			let where = {
				PROJECT_ID: projectId
			};

			// 构建排序条件 - 按时间倒序
			let orderBy = {
				FLOW_TIME: 'desc'
			};

			// 构建projection
			let fields = 'FLOW_ID,FLOW_TIME,FLOW_STATUS,FLOW_TARGET_STATUS,FLOW_EVENT,FLOW_TITLE,FLOW_NOTE,FLOW_OPERATOR_ID,FLOW_OPERATOR_NAME,FLOW_RESOURCES';

			// 尝试直接访问research_flow_history表
			const Model = dbUtil.getModel('research_flow_history');
			
			// 查询数据
			let result = await Model.getAll(where, fields, orderBy);
			
			// 处理结果
			let list = result || [];

			// 格式化时间
			for (let i = 0; i < list.length; i++) {
				let item = list[i];
				item.FLOW_TIME_DISPLAY = timeUtil.timestamp2Time(item.FLOW_TIME);
			}

			return { code: 0, data: list };
		} catch (err) {
			console.error('获取项目流程历史失败:', err);
			return { code: 500, msg: '服务器错误, ' + err.message };
		}
	}

}

module.exports = ProjectFlowController; 